package com.quan.system.common.job;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.common.enums.SchedulerStatus;
import com.quan.common.utils.DateUtils;
import com.quan.system.common.amqp.producer.ProducerMessageHTDQMailNotify;
import com.quan.system.entity.SystemJob;
import com.quan.system.service.SystemJobService;

import lombok.extern.slf4j.Slf4j;

/**
 * 员工合同到期提醒作业类
 * 
 * @author Administrator
 *
 */
@Slf4j
@Component
public class HTDQJob implements Job, Serializable {

	private static final long serialVersionUID = 1L;
	
	@Autowired
	private ProducerMessageHTDQMailNotify producerMessageHTDQMailNotify;

	@Autowired
	private SystemJobService jobService;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		JobDetail jobDetail = context.getJobDetail();

		//参数
		Map<String, Object> params = new HashMap<String, Object>();

		// JobDataMap数据
		Set<Entry<String, Object>> entrySet = jobDetail.getJobDataMap().entrySet();
		for (Entry<String, Object> entry : entrySet) {
			params.put(entry.getKey(), entry.getValue());
		}

		this.producerMessageHTDQMailNotify.sender(params);
		log.info("==>【开始执行任务】员工合同到期提醒：{}, {}, {}", DateUtils.getDateTime(), jobDetail.getKey(), jobDetail.getJobClass());
		
		
		// 修改定时任务的状态为完成
		QueryWrapper<SystemJob> queryWrapper = new QueryWrapper<SystemJob>() ;
		queryWrapper.eq("job_name", jobDetail.getKey().getName());
		SystemJob entity = this.jobService.getOne(queryWrapper);
		if(null != entity) {
			this.jobService.changeStatus(entity.getId(), SchedulerStatus.FINISHED.getName());
		}
		

	}
}
